package edu.udo.sopra10.chaturaji.gui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

import edu.udo.sopra10.chaturaji.controllers.GameController;
import edu.udo.sopra10.chaturaji.events.BoardResetEvent;
import edu.udo.sopra10.chaturaji.events.BoardResetEventListener;
import edu.udo.sopra10.chaturaji.events.MoveEvent;
import edu.udo.sopra10.chaturaji.events.MoveEventListener;
import edu.udo.sopra10.chaturaji.events.PassEvent;
import edu.udo.sopra10.chaturaji.events.PassEventListener;

/**
 * LogFrame to display the game log.
 * 
 * @author Patrick Westerhoff
 */
public class LogFrame extends JFrame implements BoardResetEventListener, MoveEventListener, PassEventListener
{
	private static final long serialVersionUID = -7832884442332173439L;
	
	private JTable logTable;
	private LogTableModel logTableModel;
	
	/**
	 * Constructs a LogFrame.
	 * 
	 */
	public LogFrame ()
	{
		super( "Logbuch" );
		
		logTableModel = new LogTableModel( new ArrayList<String>() );
		logTable = new JTable( logTableModel );
		logTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		logTable.setShowGrid( true );
		logTable.setRowSelectionAllowed( true );
		logTable.setColumnSelectionAllowed( false );
		logTable.getColumnModel().getColumn( 0 ).setMaxWidth( 25 );
		
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		renderer.setHorizontalAlignment( SwingConstants.RIGHT );
		logTable.getColumnModel().getColumn( 0 ).setCellRenderer( renderer );
		
		JPanel mainPanel = new JPanel( new BorderLayout() );
		mainPanel.add( logTable, BorderLayout.CENTER );
		this.setContentPane( mainPanel );
		this.setSize( 200, 400 );
	}
	
	@Override
	public void onBoardResetEvent ( BoardResetEvent event )
	{
		logTableModel.setLogEntries( GameController.getLog() );
	}
	
	@Override
	public void onMoveEvent ( MoveEvent event )
	{
		logTableModel.setLogEntries( GameController.getLog() );
	}
	
	@Override
	public void onPassEvent ( PassEvent event )
	{
		logTableModel.setLogEntries( GameController.getLog() );
	}
	
	/**
	 * Customized {@link TableModel} that is specialized on displaying log
	 * data.
	 * 
	 */
	private class LogTableModel extends AbstractTableModel
	{
		private static final long serialVersionUID = 4600382224765323440L;
		private List<String> logEntries;
		
		/**
		 * Constructs a LogTableModel with the given log data.
		 * 
		 * @param data the log data.
		 */
		public LogTableModel ( List<String> data )
		{
			this.logEntries = data;
		}
		
		/**
		 * Replaces the log data.
		 * 
		 * @param data the log data.
		 */
		public void setLogEntries ( List<String> data )
		{
			this.logEntries = data;
			this.fireTableDataChanged();
		}
		
		@Override
		public int getRowCount ()
		{
			return logEntries.size();
		}
		
		@Override
		public int getColumnCount ()
		{
			return 2;
		}
		
		@Override
		public String getColumnName ( int column )
		{
			return "";
		}
		
		@Override
		public Object getValueAt ( int row, int column )
		{
			if ( column == 0 )
				return logEntries.size() - row;
			return logEntries.get( logEntries.size() - row - 1 );
		}
		
		@Override
		public void setValueAt ( Object aValue, int row, int column )
		{
			return;
		}
	}
}